package com.tensquare.search.controller;

import com.tensquare.search.dao.pojo.Article;
import com.tensquare.search.server.ArticleSearchService;
import dto.ResultDTO;
import dto.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleSearchController {
    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 保存文章到es中
     */
    @PostMapping
    public ResultDTO save(@RequestBody Article article) {
        this.articleSearchService.save(article);
        return new ResultDTO(true, StatusCode.OK, "保存成功");
    }

    /**
     * 查询文章到
     */
    @GetMapping("/search/{keywords}/{page}/{size}")
    public ResultDTO listByKeyWordsLike(
            @PathVariable String keywords,
            @PathVariable int page,
            @PathVariable int size
    ) {
        Page<Article> articles = this.articleSearchService.findArticle(keywords, page, size);
        return  new ResultDTO(true, StatusCode.OK, "查询成功", articles.getContent());

    }
}

